import { IQueryObject } from 'tiny-entity2/queryObject';
import { Transaction } from "tiny-entity2";
import { ApiDataContext } from "../domain/apiDataContext";
import { DataRPermits } from "../domain/models/dataRPermits";
import { BaseService } from "./baseService";

export class DataRPermitsService extends BaseService {
    DeleteDataRPermitsByDataRegisterId(id: string, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            await ctx.Delete<DataRPermits>(x => x.dataRegisterId == $args1, ctx.DataRPermits, { $args1: id });
        });
    }
    CreateDataRPermits(entities: DataRPermits[], ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let datas: DataRPermits[] = [];
            for (let item of entities) {
                datas.push(new DataRPermits().ConverToEntity(item));
            }

            return ctx.CreateBatch(datas);

        });
    }
    UpdateDataRPermits(entity: DataRPermits, ctx?: ApiDataContext) {
        ctx || (ctx = new ApiDataContext());
        return Transaction(ctx, async (ctx) => {
            let d = new DataRPermits();
            d.ConverToEntity(entity);
            return ctx.Update(d);
        });
    }
    GetDataRPermitsById(id: string) {
        let ctx = new ApiDataContext();
    }
    QueryDataRPermits(params: IDataRPermitQueryParams) {
        let ctx = new ApiDataContext();
        let q = ctx.DataRPermits as IQueryObject<DataRPermits>;
        if (params) {
            if (params.dataRegisterId) {
                q = q.Where(x => x.dataRegisterId == $args1, { $args1: params.dataRegisterId });
            }
            if (params.accountId) {
                q = q.Where(x => x.accountId == $args1, { $args1: params.accountId });
            }
            if (params.orgId) {
                q = q.Where(x => x.orgId == $args1, { $args1: params.orgId });
            }
        }

        return q.ToList();
    }
}

export interface IDataRPermitQueryParams {
    dataRegisterId?: string;
    accountId?: string;
    orgId?: string;
}